package model

import (
	"context"
	"gorm.io/gorm"
	"order/internal/svc"
)

type Goods struct {
	gorm.Model
	GoodsName   string  `json:"goods-name" gorm:"type:varchar(20);not null;"`
	GoodsStock  int     `json:"goods-stock" gorm:"type:int(11);not null;"`
	GoodsPrice  float64 `json:"goods-price" gorm:"type:decimal(10,2);not null;"`
	GoodsImage  string  `json:"goods-image" gorm:"type:varchar(200);not null;"`
	GoodsStatus int     `json:"goods-status" gorm:"type:tinyint(1);not null;"`

	ctx    context.Context     `gorm:"-"`
	svcCtx *svc.ServiceContext `gorm:"-"`
}

func NewGoodsModel(ctx context.Context, svcCtx *svc.ServiceContext) *Goods {
	return &Goods{ctx: ctx, svcCtx: svcCtx}
}

func (g *Goods) GoodsList(name string) (info []Goods, err error) {
	return info, WithMysql(func(db *gorm.DB) error {
		return db.Where("goods_name like ?", name).Find(&info).Error
	})
}

func (g *Goods) GoodsListByPage(page, pageSize int) (info []Goods, err error) {
	return info, WithMysql(func(db *gorm.DB) error {
		offset := (page - 1) * pageSize
		return db.Table("goods").Limit(pageSize).Offset(offset).Find(&info).Error
	})
}
